package com.dgut.cbd.finalexp.myController;

import com.dgut.cbd.finalexp.dao.RecordDao;
import com.dgut.cbd.finalexp.dao.UserDao;
import com.dgut.cbd.finalexp.dto.R;
import com.dgut.cbd.finalexp.dto.RecordInfo;
import com.dgut.cbd.finalexp.entity.MyUser;
import com.dgut.cbd.finalexp.entity.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Program: finalexp
 * @Description:
 * @Author: cbd
 * @Create: 2021-06-21
 * @Version: 1.0.0
 **/
@Controller
public class UserController {


    @GetMapping("/user/register")
    public String register(){
        return "user/register";
    }

    @RequestMapping("/user/login")
    public String loginPage() {
        return "user/login";
    }

    @RequestMapping("/user/record")
    public String recordPage() {
        return "user/record";
    }

    @Controller
    static class UserLogicController {

        @Autowired
        UserDao userDao;

        @Autowired
        RecordDao recordDao;

        @PostMapping("/user/register")
        public String userRegister(@ModelAttribute  MyUser myUser){
            if (!myUser.getPassword().equals(myUser.getPasswordEnsure())){
                return "/user/login?error";
            }

            boolean b = userDao.insertOneUser(myUser);
            return  "/user/login";
        }

        @RequestMapping("/user/submit")
        @ResponseBody
        public R userSubmit(RecordInfo recordInfo,
                            @AuthenticationPrincipal(expression = "username") String id) {
            if (!id.equals(recordInfo.getStuId())) {
                return R.success("fake identity");
            }

            Record record = new Record(recordInfo);

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = formatter.format(record.getDate());
            record.setDateString(dateString);

            Record recordToday = recordDao.getOneRecordByDateAndId(record);

            if (recordToday != null) {
                return R.success("今日已打");
            }

            boolean b = recordDao.insertRecord(record);
            return R.success(recordInfo);
        }

    }

    @RestController
    static class UserRestController {

        @Autowired
        UserDao userDao;

        @Autowired
        RecordDao recordDao;

        @RequestMapping("/user/getBaseInfo")
        public R getBaseInfo(@AuthenticationPrincipal(expression = "username") String id) {

            MyUser myUser = userDao.loadUserFromId(id);
            myUser.setPassword(null);
            return R.success(myUser);
        }

        @RequestMapping("/user/getNewestRecord")
        public R getRecord(@AuthenticationPrincipal(expression = "username") String id) {

            Date date = new Date();

            Record record = new Record();
            record.setDate(date);
            record.setStuId(id);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = formatter.format(date);
            record.setDateString(dateString);

            record = recordDao.getOneRecordByDateAndId(record);
            if (record == null) {
                Calendar instance = Calendar.getInstance();
                instance.setTime(date);
                instance.add(Calendar.DAY_OF_MONTH, -1);
                date = instance.getTime();
                dateString = formatter.format(date);

                record = new Record();
                record.setDate(date);
                record.setStuId(id);

                record.setDateString(dateString);

                record = recordDao.getOneRecordByDateAndId(record);
            }
            if (record == null) {
                return R.success(null);
            }

            return R.success(new RecordInfo(record));

        }

    }

}
